package conf

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
	"strings"
)

var (
	Config *viper.Viper

	Filename  string
	Sheet     string
	Allsheet  string
	Checkrate int

	ColIdNo          int
	ColModuleNo      int
	ColUrlNo         int
	ColMethodNo      int
	ColHeadersNo     int
	ColBodyNo        int
	ColDescNo        int
	ColExpectCodeNo  int
	ColExpectDataNo  int
	ColExtractNo     int
	Domain           string
	CronSpecMorning  string
	CronSpecInterval string
	Mode             string
	LogMode          string

	DingtalkUrl  string
	Webhook      string
	Secret       string
	Env          string
	DingtalkSend string

	Retry int
)

//
//  init
//  @Description: 在main中import此conf包时，就自动执行init函数，读取yaml配置文件
//
func init() {
	//读取配置文件
	path, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	Config = viper.New()
	Config.AddConfigPath(path)     //设置读取的文件路径
	Config.SetConfigName("config") //设置读取的文件名
	Config.SetConfigType("yaml")   //设置文件的类型
	//尝试进行配置读取
	if err := Config.ReadInConfig(); err != nil {
		panic(err)
	}
	//读取并打印文件读取出来的内容:
	fmt.Println("[读取接口文件] " + Config.GetString("datasource.file"))

	//读取配置中的excel字段行号，存入全局变量
	LogMode = Config.GetString("log_mode")
	Mode = Config.GetString("mode")
	Retry = Config.GetInt("retry")
	Filename = Config.GetString("datasource.file")
	Sheet = Config.GetString("excel.sheet")
	Allsheet = Config.GetString("excel.allsheet")
	Checkrate = Config.GetInt("checkrate")
	ColUrlNo = Config.GetInt("excel.column.url")
	ColMethodNo = Config.GetInt("excel.column.method")
	ColHeadersNo = Config.GetInt("excel.column.headers")
	ColBodyNo = Config.GetInt("excel.column.body")
	ColDescNo = Config.GetInt("excel.column.desc")
	ColExpectCodeNo = Config.GetInt("excel.column.expectcode")
	ColExpectDataNo = Config.GetInt("excel.column.expectdata")
	ColExtractNo = Config.GetInt("excel.column.extract")
	Domain = Config.GetString("domain.url")
	Domain = strings.TrimRight(Domain, "/")
	CronSpecMorning = Config.GetString("cron_spec.morning")
	CronSpecInterval = Config.GetString("cron_spec.interval")
	DingtalkUrl = Config.GetString("dingtalk.url")
	Webhook = Config.GetString("dingtalk.webhook")
	Secret = Config.GetString("dingtalk.secret")
	Env = Config.GetString("dingtalk.title")
	DingtalkSend = Config.GetString("dingtalk.send")
}
